package com.hx.er.action.admin.settings;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.CountryBean;
import com.hx.er.bean.detail.CityDetailBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminSettingsService;

public class AdminCityAddAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String CITY_NAME_UNIQUE_MSG = "admin.cityList.message.add.unique.cityName";
	public static final String CITY_NAME_EMPTY_MSG = "admin.cityList.message.add.empty.cityName";
	public static final String CITY_NAME_NOT_LETTERS = "admin.cityList.message.add.letters.cityName";
	public static final String LOCAL_CITY_NAME_EMPTY_MSG = "admin.cityList.message.add.empty.localCityName";

	private CityDetailBean cityDetailBean;
	private String updateDateStr;

	@Autowired
	private IAdminSettingsService adminSettingsService;

	public String init() {
		this.cityDetailBean = new CityDetailBean();
		
		long countryId = Long.parseLong(ErProperties.getProperty(Constants.PROPERTY_DEFAULT_COUNTRY));
		CountryBean country = this.adminSettingsService.getCountryById(countryId);
		this.cityDetailBean.setCountryId(country.getCountryId());
		this.cityDetailBean.setCountryName(country.getCountryName());
		
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			return INPUT;
		}

		this.cityDetailBean.setCreateUserId(super.getUserInfoLogin().getUserId());
		this.cityDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		// 插入数据
		boolean successFlag = this.adminSettingsService.insertCityWithoutId(this.cityDetailBean);

		// 根据插入结果返回
		if (successFlag) {
			this.addActionMessage(super.getText(ADD_SUCCESS_MSG));
			return SUCCESS;
		} else {
			this.addFieldError("msg.cityName", super.getText(CITY_NAME_UNIQUE_MSG));
			return INPUT;
		}
	}

	private boolean checkParams() {
		// 城市英文名验证
		if (StringUtil.isEmpty(this.cityDetailBean.getCityName())) {
			this.addFieldError("msg.cityName", super.getText(CITY_NAME_EMPTY_MSG));
		} else if (!StringUtil.isCharsLetter(this.cityDetailBean.getCityName())) {
			this.addFieldError("msg.cityName", super.getText(CITY_NAME_NOT_LETTERS));
		}

		// 城市本地名验证
		if (StringUtil.isEmpty(this.cityDetailBean.getLocalCityName())) {
			this.addFieldError("msg.localCityName", super.getText(LOCAL_CITY_NAME_EMPTY_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public CityDetailBean getCityDetailBean() {
		return cityDetailBean;
	}

	public void setCityDetailBean(CityDetailBean cityDetailBean) {
		this.cityDetailBean = cityDetailBean;
	}

	public String getUpdateDateStr() {
		return updateDateStr;
	}

	public void setUpdateDateStr(String updateDateStr) {
		this.updateDateStr = updateDateStr;
	}

}
